import { sources, blogTypes, blogStates } from 'common/utils/classify'
import { addBlog, updateBlog, getBlogList, delBlog } from 'api/article'

const blog = {
  state: {
    sources,
    blogTypes,
    blogStates,
    blogInfo: {},
    list: [],
    total: 0
  },
  mutations: {
    SET_BLOGLIST(state, data) {
      state.list = data.list
      state.total = data.total
    },
    SET_BLOGINFO(state, info) {
      state.blogInfo = info
    }
  },
  actions: {
    // 添加文章
    AddBlog({ commit }, info) {
      return new Promise((resolve, reject) => {
        addBlog(info).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetBlogList({ commit }, params) {
      return new Promise((resolve, reject) => {
        getBlogList(params).then(response => {
          commit('SET_BLOGLIST', response.data)
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    CurrentBlogInfo({ commit }, info) {
      commit('SET_BLOGINFO', info)
    },
    DelBlog({ commit }, id) {
      return new Promise((resolve, reject) => {
        delBlog({ id: id }).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    },

    UpdateBlog({ commit }, info) {
      return new Promise((resolve, reject) => {
        updateBlog(info).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
export default blog
